import merge from 'lodash/merge'
import { routerReducer as routing } from 'react-router-redux'
import { combineReducers } from 'redux'
import  * as actions  from '../actions/login'
import storage from '../libs/utils/storage'

function login(state = { user: {}, isFetching: false }, action) {
  if (action.type == actions.USER_LOGIN_SUCCESS) {
    let nextState = Object.assign({}, state, {
        user: {
          employeeCode: action.employeeCode,
          token: action.token
        },
        isFetching: false
      })
    storage.save('user', nextState.user, true, 'YYOA')
    return nextState
  } else if (action.type == actions.USER_LOGIN_REQUEST) {
    return Object.assign({}, state, {
      isFetching: true
    })
  }
  return state
}

const rootReducer = combineReducers({
  login,
  routing
})

export default rootReducer